<template>
	<view class="wallet-container">
		<!-- 顶部余额区域 -->
		<view class="balance-header">
			<view class="balance-info">
				<text class="balance-label">我的余额</text>
				<text class="balance-amount">{{ money | formatMoney }}</text>
			</view>
			<button class="recharge-btn" @click="goToRecharge">充值</button>
		</view>

		<!-- 消费明细区域 -->
		<view class="consumption-list">
			<text class="list-title">明细</text>

			<!-- 明细列表 -->
			<view class="record-item" v-for="item in consumptionList" :key="item.id">
				<view class="record-icon">
					<image src="/static/androidImg/icon_pay_add.png" v-if="item.type==0" class="icon-img"></image>
					<image src="/static/androidImg/icon_pay_wechat.png" v-if="item.type==1" class="icon-img"></image>
					<image src="/static/androidImg/icon_pay_zhi.png" v-if="item.type==2" class="icon-img"></image>

				</view>
				<view class="record-info">
					<!-- type": 0,//消费类型0：商品消费；1：微信充值；2：支付宝充值 -->
					<text class="record-title" v-if="item.type==0">商品消费</text>
					<text class="record-title" v-if="item.type==1">微信充值</text>
					<text class="record-title" v-if="item.type==2">支付宝充值</text>
					<text class="record-time">{{ formatTime(item.create_time) }}</text>
				</view>
				<text class="record-amount">{{ item.type != 0 ? '+' : '-' }}{{ item.money | formatMoney }}</text>
			</view>

			<!-- 加载更多 -->
			<view class="load-more" v-if="hasMore && !loading">
				<text>上拉加载更多</text>
			</view>
			<view class="loading-state" v-if="loading">
				<text>加载中...</text>
			</view>
			<view class="empty-state" v-if="consumptionList.length === 0 && !loading">
				<image src="/static/androidImg/icon_not_data.png" class="empty-img"></image>
				<text class="empty-text">暂无消费记录</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getUserInfo,
		getUserConsumptionRecord
	} from '../../api/request.js';

	export default {
		data() {
			return {
				money: 0, // 用户余额
				consumptionList: [], // 消费记录列表
				page: 1, // 当前页码
				pageSize: 10, // 每页条数
				total: 0, // 总记录数
				hasMore: true, // 是否有更多数据
				loading: false // 加载状态
			};
		},

		onLoad() {
			// 页面加载时获取用户信息和消费记录
			this.getUserInfo();
			this.getConsumptionRecords();
		},

		onReachBottom() {
			// 上拉加载更多消费记录
			if (this.hasMore && !this.loading) {
				this.page++;
				this.getConsumptionRecords();
			}
		},

		filters: {
			// 金额格式化过滤器
			formatMoney(value) {
				return '¥' + parseFloat(value).toFixed(2);
			}
		},

		methods: {
			// 获取用户信息（余额）
			getUserInfo() {
				getUserInfo().then(res => {
					if (res.code === 1 && res.data) {
						this.money = res.data.money || 0;
					} else {
						uni.showToast({
							title: res.msg || '获取余额失败',
							icon: 'none'
						});
					}
				}).catch(err => {
					console.error('获取用户信息失败:', err);
					uni.showToast({
						title: err.msg || '网络错误',
						icon: 'none'
					});
				});
			},

			// 获取消费记录列表
			getConsumptionRecords() {
				if (this.loading) return;

				this.loading = true;

				getUserConsumptionRecord({
					page: this.page,
					limit: this.pageSize
				}).then(res => {
					if (res.code === 1 && res.data) {
						const newRecords = res.data.rows || [];

						if (this.page === 1) {
							this.consumptionList = newRecords;
						} else {
							this.consumptionList = [...this.consumptionList, ...newRecords];
						}

						this.total = res.data.total || 0;
						this.hasMore = this.consumptionList.length < this.total;
					} else {
						uni.showToast({
							title: res.msg || '获取消费记录失败',
							icon: 'none'
						});
					}
				}).catch(err => {
					console.error('获取消费记录失败:', err);
					uni.showToast({
						title: err.msg || '网络错误',
						icon: 'none'
					});
				}).finally(() => {
					this.loading = false;
				});
			},

			// 跳转到充值页面
			goToRecharge() {
				uni.navigateTo({
					url: '/pages/my/recharge'
				});
			},

			// 根据记录类型获取图标
			getRecordIcon(type) {
				// 根据实际业务类型返回不同图标
				return '/static/androidImg/icon_pay_add.png';
			},

			// 格式化时间
			formatTime(timeStr) {
				if (!timeStr) return '';
				// 简化处理，实际项目中可使用更完善的时间格式化
				return timeStr;
			}
		}
	};
</script>

<style lang="scss">
	.wallet-container {
		min-height: 100vh;
		background-color: #f5f5f5;

		.balance-header {
			background: linear-gradient(135deg, #ff7a00, #ff5000);
			padding: 60rpx 30rpx 40rpx;
			color: #fff;
			position: relative;

			.balance-label {
				font-size: 28rpx;
				opacity: 0.9;
				margin-bottom: 10rpx;
				display: block;
			}

			.balance-amount {
				font-size: 60rpx;
				font-weight: bold;
				margin-bottom: 30rpx;
				display: block;
			}

			.recharge-btn {
				position: absolute;
				right: 30rpx;
				top: 50%;
				transform: translateY(-50%);
				background-color: #fff;
				color: #ff6b00;
				border: none;
				padding: 15rpx 30rpx;
				border-radius: 30rpx;
				font-size: 28rpx;
			}
		}

		.consumption-list {
			padding: 20rpx 30rpx;

			.list-title {
				font-size: 32rpx;
				font-weight: bold;
				color: #333;
				margin-bottom: 20rpx;
				display: block;
			}

			.record-item {
				display: flex;
				align-items: center;
				padding: 25rpx 0;
				border-bottom: 1rpx solid #eee;

				.record-icon {
					width: 60rpx;
					height: 60rpx;
					margin-right: 20rpx;
					display: flex;
					align-items: center;
					justify-content: center;

					.icon-img {
						width: 40rpx;
						height: 40rpx;
					}
				}

				.record-info {
					flex: 1;

					.record-title {
						font-size: 30rpx;
						color: #333;
						margin-bottom: 5rpx;
						display: block;
					}

					.record-time {
						font-size: 24rpx;
						color: #999;
					}
				}

				.record-amount {
					font-size: 30rpx;
					font-weight: bold;
					color: #ff4d4f;
				}
			}

			.load-more,
			.loading-state,
			.empty-state {
				text-align: center;
				padding: 30rpx 0;
				color: #999;
				font-size: 28rpx;
			}

			.empty-state {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding-top: 150rpx;

				.empty-img {
					width: 180rpx;
					height: 180rpx;
					margin-bottom: 20rpx;
					opacity: 0.6;
				}
			}
		}
	}
</style>